﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace Fortunate.API.BASE
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]
    [AllowAnonymous]
    public abstract class BaseApiController : ControllerBase
    {
        /// <summary>
        /// Guid
        /// </summary>
        protected Guid GetGuid => Guid.NewGuid();

        /// <summary>
        /// 数字转换（1 = 一）
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        protected string ConversionDigit(int value)
        {
            switch (value)
            {
                case 0:
                    return "";
                case 1:
                    return "一";
                case 2:
                    return "二";
                case 3:
                    return "三";
                case 4:
                    return "四";
                case 5:
                    return "五";
                case 6:
                    return "六";
                case 7:
                    return "七";
                case 8:
                    return "八";
                case 9:
                    return "九";
                default:
                    return "十";
            }
        }

        /// <summary>
        /// 获取时间戳
        /// </summary>
        protected long GetTimestamp => new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();

        /// <summary>
        /// 获取格式为《年-月-日》
        /// </summary>
        protected string GetDate => DateTime.Now.ToString("yyyy-MM-dd");

        /// <summary>
        /// 获取《年月日时分秒》
        /// </summary>
        protected string GetDateTime => DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        /// <summary>
        /// 返回成功
        /// </summary>
        /// <param name="data">数据集 object</param>
        /// <param name="Message">提示信息</param>
        /// <returns></returns>
        protected IActionResult Success(object? data = null, string Message = "")
        {
            return Ok(Fortunate.ViewModel.ResponseInput.Response.Correct(data, Message));
        }

        /// <summary>
        /// 自定义弹出信息返回
        /// </summary>
        /// <param name="Message"></param>
        /// <returns></returns>
        protected IActionResult Fail(string Message = "")
        {
            return Ok(Fortunate.ViewModel.ResponseInput.Response.Mistake(Message));
        }

        /// <summary>
        /// 获取月的天数
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <returns></returns>
        protected int MonthDays(int year, int month)
        {
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                case 2:
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                        return 29;//闰年
                    else
                        return 28;
                default:
                    return 30;
            }
        }
    }
}
